/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.impl');
(function () {
	
	var EVENTS = {
		READY: 'ready',
		LOAD_EXCEPTION: 'loadexception'
	};
	
	/**
	 * @class EventBusImpl
	 * @singleton
	 * @namespace net.autodata.impl
	 */
	var EventBusImpl = Ext.extend(Object, {
		
		/**
		 * @event ready
		 * Fired when the Session object has been initalized
		 */
		
		/**
		 * @event loadexception
		 * Fired when a data call does not execute successfully
		 */
		
		/**
		 * @method constructor
		 */
		constructor: function () 
		{
			ASC.log('net.autodata.EventBus->constructor');
			
			var events = new Ext.util.Observable();
			var eventsToAdd = this.getEvents();
			
			if (eventsToAdd !== undefined) {
				events.addEvents(eventsToAdd);
			}
			
			return {			
				
				/**
				 * Fires the specified event with the passed parameters (minus the event name).
				 * An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget})
				 * by calling {@link #enableBubble}.
				 * @method fireEvent
				 * @param {String} eventName The name of the event to fire.
				 * @param {Object...} args Variable number of parameters are passed to handlers.
				 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.
				 */
				fireEvent: function ()
				{
					events.fireEvent.apply(events, arguments);
				},
				
				/**
				 * Appends an event handler to this object
				 * @method on
				 * @param {String}   eventName     The type of event to listen for
				 * @param {Function} handler       The method the event invokes
				 * @param {Object}   scope         (optional) The scope (this reference) in which the handler function is executed.
				 * If omitted, defaults to the object which fired the event.
				 * @param {Object}   options       (optional) An object containing handler configuration.
				 */
				on: function ()
				{
					events.on.apply(events, arguments);
				},
				
				/**
				 * Resume firing events. (see {@link #suspendEvents})
				 * If events were suspended using the queueSuspended parameter, then all
				 * events fired during event suspension will be sent to any listeners now.
				 * @method resumeEvents
				 */
				resumeEvents: function ()
				{
					events.resumeEvents.apply(events, arguments);
				},
				
				/**
				 * Suspend the firing of all events. (see {@link #resumeEvents})
				 * @method suspendEvents
				 * @param {Boolean} queueSuspended Pass as true to queue up suspended events to be fired
				 * after the {@link #resumeEvents} call instead of discarding all suspended events;
				 */
				suspendEvents: function ()
				{
					events.suspendEvents.apply(events, arguments);
				},
				
				/**
				 * Removes an event handler
				 * @method un
				 * @param {String}   eventName     The type of event the handler was associated with.
				 * @param {Function} handler       The handler to remove. This must be a reference to the function passed into the {@link #addListener} call.
				 * @param {Object}   scope         (optional) The scope originally specified for the handler.
				 * @method
				 */
				un: function ()
				{
					events.un.apply(events, arguments);
				}
			};
		},
		
		/**
		 * Returns a list of events to be added to the net.autodata.EventBus
		 * @method getEvents
		 * @returns Array
		 */
		getEvents: function ()
		{
			var events = {};
			events[EVENTS.READY] = true;
			return events;
		}
	});
	net.autodata.impl.EventBusImpl = EventBusImpl;
	
	Ext.ns('net.autodata');
	net.autodata.EventBus = {
		getInstance: function ()
		{
			var instance = undefined;
			return function () {
				if (!instance) {
					instance = new EventBusImpl();
				}
				
				return instance;
			}
		}(),
		
		EVENTS: EVENTS
	};
		
})();